//
//  MainTabBarViewController.m
//  BZZhangZhongFo
//
//  Created by 郭艺伟 on 2017/10/9.
//  Copyright © 2017年 郭艺伟. All rights reserved.
//

#import "MainTabBarViewController.h"
#import <BZBaseViewComponent/BZMainTabBar.h>
#import <BZBaseViewComponent/BZBaseNavigationController.h>
#import <BZBaseViewComponent/BZBaseViewController.h>
#import <BZMeComponent/BZMeViewController.h>

@interface MainTabBarViewController ()

/**
 *  自定义工具栏
 */
@property (nonatomic, weak) BZMainTabBar *customTabBar;

@end

@implementation MainTabBarViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    // 初始化tabbar
    [self setupTabbar];
    
    // 初始化所有的子控制器
    [self setupAllChildViewControllers];
}

- (void )viewDidLayoutSubviews
{
    [super viewDidLayoutSubviews];
    
    self.customTabBar.frame = self.tabBar.bounds;
}


- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    // 删除系统自动生成的UITabBarButton
    for (UIView *child in self.tabBar.subviews) {
        if ([child isKindOfClass:[UIControl class]]) {
            [child removeFromSuperview];
        }
    }
}

/**
 *  初始化tabbar
 */
- (void)setupTabbar
{
    BZMainTabBar *customTabBar = [[BZMainTabBar alloc] init];
    customTabBar.frame = self.tabBar.bounds;
    customTabBar.delegate = self;
    [self.tabBar addSubview:customTabBar];
    self.customTabBar = customTabBar;
}

#pragma mark - tabbar的代理方法
/**
 *  监听tabbar按钮的改变
 *  @param from   原来选中的位置
 *  @param to     最新选中的位置
 */
- (void)tabBar:(BZMainTabBar *)tabBar didSelectedButtonFrom:(int)from to:(int)to
{
    self.selectedIndex = to;
    
    if (to == 0) { // 点击了首页
        //[self.homeVC refresh];
    }
}

/**
 *  监听加号按钮点击
 */
- (void)tabBarDidClickedPlusButton:(BZMainTabBar *)tabBar
{
    //    IWComposeViewController *compose = [[IWComposeViewController alloc] init];
    //    BaseNavigationController *nav = [[BaseNavigationController alloc] initWithRootViewController:compose];
    //    [self presentViewController:nav animated:YES completion:nil];
}

/**
 *  初始化所有的子控制器
 */
- (void)setupAllChildViewControllers
{
    // 1.首页
    BZBaseViewController *homeVC = [[BZBaseViewController alloc] init];
    [self setupChildViewController:homeVC title:@"首页" imageName:@"firstTabBar" selectedImageName:@"firstTabBar_S"];
    
    
    // 2.发现
    BZBaseViewController *findVC = [[BZBaseViewController alloc] init];
    [self setupChildViewController:findVC title:@"发现" imageName:@"secondTabBar" selectedImageName:@"secondTabBar_S"];
    
    // 3.消息
    BZBaseViewController *messageVC = [[BZBaseViewController alloc] init];
    [self setupChildViewController:messageVC title:@"消息" imageName:@"thirdTabBar" selectedImageName:@"thirdTabBar_S"];
    
    
    // 4.我
    BZMeViewController *meVC = [[BZMeViewController alloc] init];
    [self setupChildViewController:meVC title:@"我" imageName:@"fourthTabBar" selectedImageName:@"fourthTabBar_S"];
}

/**
 *  初始化一个子控制器
 *
 *  @param childVc           需要初始化的子控制器
 *  @param title             标题
 *  @param imageName         图标
 *  @param selectedImageName 选中的图标
 */
- (void)setupChildViewController:(UIViewController *)childVc title:(NSString *)title imageName:(NSString *)imageName selectedImageName:(NSString *)selectedImageName
{
    // 1.设置控制器的属性
    childVc.title = title;
    // 设置图标
    childVc.tabBarItem.image = [UIImage imageNamed:imageName];
    
    // 设置选中的图标
    UIImage *selectedImage = [UIImage imageNamed:selectedImageName];
    
    childVc.tabBarItem.selectedImage = [selectedImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
#warning 都包装一个基础导航控制器
    // 2.包装一个导航控制器
    BZBaseNavigationController *nav = [[BZBaseNavigationController alloc] initWithRootViewController:childVc];
    [self addChildViewController:nav];
    //nav.navigationBar.translucent=NO;
    
    // 3.添加tabbar内部的按钮
    [self.customTabBar addTabBarButtonWithItem:childVc.tabBarItem];
}


@end
